<?php

use hhz\constant\redis\redisKeys;
use Biz\cache\album\AlbumRecommendCache;
use Biz\model\Album\AlbumListModel;
use Biz\service\Album\AlbumListService;
use Hhz\Redis\newCache;

class DelAlbumAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;

    /**
     * @throws \Exception
     */
    public function main()
    {
        $currUid = $this->uid;
        $albumId = trim($this->getRequest()->getPost('album_id', ''));

        if (!$currUid) {
            throw new \Exception("请登录重试！");
        }
        if (empty($albumId)) {
            throw new Exception('album_id参数缺失');
        }
        $albumInfo = AlbumListModel::getByWhere([
            'album_id' => $albumId,
            'status[!]' => AlbumListModel::STATUS_DELETE,
        ]);
        if (empty($albumInfo)) {
            throw new Exception('清单不存在');
        }
        if ($albumInfo['uid'] != $currUid) {
            throw new Exception('仅可以删除自己的清单');
        }

        $newRedis = new newCache(redisKeys::KEY_B_CACHE_ALBUM_RECOMMEND_OFFICIAL_LIST);
        $aData = $newRedis->getCache();
        $info['album_id'] = $aData ? explode(',', $aData) : [];
        $albumIdCacheArr =$aData ? explode(',', $aData):[];

        if (in_array($albumId, $albumIdCacheArr)) {
            throw new Exception('正在被推荐，暂不支持删除');
        }
        $result = AlbumListService::delAlbum($albumId, $currUid);
        if (!$result) {
            throw new Exception('清单删除失败');
        }
        $this->setSuccess(null, self::API_CODE_SUCCES);


    }
}